package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryButton;
import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.codetable.CtDesignType;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtSendBy;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.dao.OrderButtonDAO;
import com.hilaver.dzmis.order.OrderButton;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class OrderButtonServiceImpl extends AbstractBaseService {
	
	private CtServiceImpl ctService;
	
	private OrderButtonDAO dao;
	
	public OrderButtonServiceImpl(){
		this.dao = new OrderButtonDAO();
		this.ctService = new CtServiceImpl();
	}
	
	public String edit(OrderButton obj) throws Exception {
		String operation = null;
		if(obj.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}

	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(OrderButton.class.getName(), id);
		return super.delete(OrderButton.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		OrderButton ob = (OrderButton)this.dao.get(OrderButton.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(ob));
		
		xmlSB.append(this.toXMLFragment(ob));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getWithDescription(int id) throws Exception {
		OrderButton ob = (OrderButton)this.dao.get(OrderButton.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(this.toXMLFragment(ob));

		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort,
			String order, String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(OrderButton.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((OrderButton)obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(OrderButton.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	
	public String getAll(Integer piId) throws Exception{
		
		return null;
	}

	public String getMaxReferenceIndex() throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxReferenceIndex();
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("000001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to6Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String toXMLFragment(OrderButton ob) throws Exception {
		StringBuffer xmlSB = new StringBuffer();
		
		xmlSB.append("<ctSeasonNumber>");
		xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), ob.getCtSeasonNumber(), locale));
		xmlSB.append("</ctSeasonNumber>");
		
		
		BiAccessoryButton biAccessoryButton = ob.getBiAccessoryButton();
		
		writeFile(realFileDir + biAccessoryButton.getPhoto1Name(), biAccessoryButton.getPhoto1());
		writeFile(realFileDir + biAccessoryButton.getPhoto2Name(), biAccessoryButton.getPhoto2());
		writeFile(realFileDir + biAccessoryButton.getPhoto3Name(), biAccessoryButton.getPhoto3());
		
		xmlSB.append("<ctDesignType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtDesignType.class.getName(), ob.getCtDesignType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctDesignType>");
		
		
		xmlSB.append("<biButtonId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biAccessoryButton.getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</biButtonId>");
		
		xmlSB.append("<biButtonReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biAccessoryButton.getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</biButtonReference>");
		
		if(biAccessoryButton.getBiFranceCustomer() != null){
			xmlSB.append("<customerId>");
			xmlSB.append(biAccessoryButton.getBiFranceCustomer().getId());
			xmlSB.append("</customerId>");
			
			xmlSB.append("<customerName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerName>");
			
			xmlSB.append("<customerDepartment>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getBiFranceCustomer().getDepartment()==null?"":biAccessoryButton.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerDepartment>");
			
			xmlSB.append("<customerReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getCustomerReference());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerReference>");
			
			xmlSB.append("<customerColor>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getCustomerColor());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerColor>");
		}
		
		if(biAccessoryButton.getBiFactory() != null){
			xmlSB.append("<factoryId>");
			xmlSB.append(biAccessoryButton.getBiFactory().getId());
			xmlSB.append("</factoryId>");
			
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
			
			xmlSB.append("<factoryReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getFactoryReference());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryReference>");
			
			xmlSB.append("<factoryColor>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAccessoryButton.getFactoryColor());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryColor>");

		}
		
		
		xmlSB.append("<photo1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAccessoryButton.getPhoto1Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAccessoryButton.getPhoto1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAccessoryButton.getPhoto2Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAccessoryButton.getPhoto2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo2Name>");
		
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAccessoryButton.getPhoto3Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAccessoryButton.getPhoto3Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(ob));

		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		OrderButtonServiceImpl os = new OrderButtonServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}

}
